package qc.module.sl.api.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import qc.module.sl.api.ServiceCall;
import qc.module.sl.dto.project.ProjectDto;
import qc.module.sl.dto.project.ProjectQueryConditionDto;
import qc.module.sl.dto.project.classify.ProjectClassifyDto;
import qc.module.sl.dto.project.classify.ProjectClassifyQueryConditionDto;
import qc.module.sl.service.BasicProjectClassifyService;
import qc.module.sl.service.BasicProjectsService;

import java.util.List;
import java.util.Map;

/**
 * ProjectController
 *
 * @author QuCheng Tech
 * @create 2023/1/9
 */
@RestController
@RequestMapping("/project")
public class ProjectController {
    private BasicProjectClassifyService basicProjectClassifyService;

    @Autowired
    public void setBasicProjectClassifyService(BasicProjectClassifyService basicProjectClassifyService) {
        this.basicProjectClassifyService = basicProjectClassifyService;
    }

    private BasicProjectsService basicProjectsService;

    @Autowired
    public void setBasicProjectsService(BasicProjectsService basicProjectsService) {
        this.basicProjectsService = basicProjectsService;
    }

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 查询水利工程分类信息，可用于返回启用状态的全部分类信息
     *
     * @param dto
     * @return 水利工程分类信息集合
     * @author QcCheng Tech
     * @since 2023/2/15
     */
    @GetMapping("/queryclassify")
    public List<ProjectClassifyDto> queryClassify(@RequestBody ProjectClassifyQueryConditionDto dto) {
        return basicProjectClassifyService.query(dto);
    }

    /**
     * 根据查询条件查询项目信息
     *
     * @return 项目信息集合
     * @param    dto    项目信息查询dto
     * @author QcCheng Tech
     * @since 2023/2/15
     */
    @GetMapping("/query")
    public List<ProjectDto> query(@RequestBody ProjectQueryConditionDto dto) throws Exception {
        List<ProjectDto> projectDtoList = basicProjectsService.query(dto);

        try {
            //查询结果信息不为空
            if (projectDtoList != null) {
                ServiceCall serviceCall = new ServiceCall();

                Map<String, String> adminDivisionMap = serviceCall.getAdminDivision(restTemplate, discoveryClient);

                //获取到行政区划信息不为空，进行adname字段赋值
                if (adminDivisionMap != null) {
                    for (int i = 0; i < projectDtoList.size(); i++) {
                        for (Map.Entry<String, String> entry : adminDivisionMap.entrySet()) {
                            if (!StringUtils.isBlank(projectDtoList.get(i).getAdcode()) && projectDtoList.get(i).getAdcode().equals(entry.getKey())) {
                                projectDtoList.get(i).setAdname(entry.getValue());
                                break;
                            }
                        }
                    }
                }
            }

            return projectDtoList;
        } catch (Exception exception) {
            //调用微服务出现异常时也需要返会查询的水利工程结果，只是结果中的行政区划名称为空
            return projectDtoList;
        }
    }
}
